﻿using Caliburn.Micro;
using myapp.Events;
using myapp.Events.ViewModel;
using myapp.Instances;
using myapp.Models;
using myappcore.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myapp.ViewModels
{
    /// <summary>
    /// Item List Of Possition
    /// </summary>
    public class ItemListOfPossitionViewModel : IBaseContentViewModel, IHandle<ViewModelItemSelectedEvent>
    {
        #region Attribute
        private static ItemListOfPossitionViewModel _instance;
        private Possition _posstion;
        private List<OrderDetail> _orderDetailList;
        #endregion

        #region Properties

        /// <summary>
        /// Possition
        /// </summary>
        public Possition Possition
        {
            get
            {
                return _posstion;
            }
            set
            {
                if (null != value)
                {
                    _posstion = value;
                    NotifyOfPropertyChange(() => Possition);
                }
            }
        }

        /// <summary>
        /// Order Detail List
        /// </summary>
        public IObservableCollection<OrderDetail> OrderDetailList
        {
            get
            {
                var result = new BindableCollection<OrderDetail>();
                result.AddRange(_orderDetailList);
                return result;
            }
        }

        /// <summary>
        /// Instance
        /// </summary>
        public static ItemListOfPossitionViewModel Instance
        {
            get
            {
                if (null == _instance)
                {
                    _instance = new ItemListOfPossitionViewModel();
                }
                return _instance;
            }
        }
        #endregion

        #region Constructor

        private ItemListOfPossitionViewModel()
        {
            MainInstance.Instance.EventAggregator.Subscribe(this);
            _orderDetailList = new List<OrderDetail>();
        }

        #endregion

        #region Method

        /// <summary>
        /// Add Order Detail
        /// </summary>
        /// <param name="orderDetail"></param>
        private void AddOrderDetail(OrderDetail orderDetail)
        {
            for (int index = 0; index < _orderDetailList.Count; index++)
            {
                if (orderDetail.Item.Id == _orderDetailList[index].Item.Id)
                {
                    _orderDetailList[index].Amount++;
                    NotifyOfPropertyChange(() => OrderDetailList);
                    return;
                }
            }
            _orderDetailList.Add(orderDetail);
            NotifyOfPropertyChange(() => OrderDetailList);
        }

        /// <summary>
        /// Add Order Detail
        /// </summary>
        /// <param name="orderDetail"></param>
        private void RemoveOrderDetail(OrderDetail orderDetail)
        {
            for (int index = 0; index < _orderDetailList.Count; index++)
            {
                if (orderDetail.Item.Id == _orderDetailList[index].Item.Id)
                {
                    if (_orderDetailList[index].Amount > 1)
                    {
                        _orderDetailList[index].Amount--;
                    }
                    else
                    {
                        _orderDetailList.RemoveAt(index);
                    }
                    NotifyOfPropertyChange(() => OrderDetailList);
                    return;
                }
            }
        }

        /// <summary>
        /// Handle View Model Selected Event 
        /// </summary>
        /// <param name="message"></param>
        public void Handle(ViewModelItemSelectedEvent message)
        {
            OrderDetail orderDetail = new OrderDetail()
            {
                Id = 1,
                Item = message.ItemViewModel.Item,
                Price = 10000,
                Amount = 1
            };
            AddOrderDetail(orderDetail);
        }

        /// <summary>
        /// IncreaseAmount
        /// </summary>
        /// <param name="orderDetail"></param>
        public void IncreaseAmountSelected(OrderDetail orderDetail)
        {
            AddOrderDetail(orderDetail);
        }

        /// <summary>
        /// DecreaseAmount
        /// </summary>
        /// <param name="orderDetail"></param>
        public void DecreaseAmountSelected(OrderDetail orderDetail)
        {
            RemoveOrderDetail(orderDetail);
        }
        #endregion
    }
}
